package com.topscomm.file.task;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.stereotype.Component;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.pub.util.ConvertUtil;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 文件处理任务的管理类
 * 
 * @description:
 * @author: donghang
 * @date: 2021年5月12日下午9:47:11
 * @modify:
 */
@Component
@Data
@Slf4j
public class FileProcessTaskManage {
	/**
	 * 线程池调度
	 */
	private ExecutorService executorService;
	/**
	 * 线程安全list
	 */
	private List<Map<String, Object>> processList = new CopyOnWriteArrayList<Map<String, Object>>();
	/**
	 * 同时在处理的文件
	 */
	private int maxProcessList = 20;

	/**
	 * 
	 * @description:
	 * @author: donghang
	 * @date: 2021年5月12日下午9:47:53
	 * @modify:
	 */
	public FileProcessTaskManage() {
		// 线程池调度
		Integer availableProcessors = Runtime.getRuntime().availableProcessors();
		Integer numOfThreads = availableProcessors * 2;
		executorService = Executors.newCachedThreadPool();
		// new ThreadPoolExecutor(numOfThreads, numOfThreads, 60L,
		// TimeUnit.SECONDS,
		// new LinkedBlockingDeque<Runnable>(60));
	}

	/**
	 * 
	 * @description:
	 * @param filePath
	 * @param targetFilePath
	 * @param identifier
	 * @return
	 * @author: donghang
	 * @date: 2021年5月12日下午9:49:23
	 * @modify:
	 */
	public boolean handle(Map<String, Object> notification) {
		try {
			// 判断文件是否在处理中
			if (!this.isExistHandleringFile(notification)) {
				if (this.processList.size() > this.maxProcessList) {
					throw new ServiceException("服务器当前文件处理压力过大，请几分钟后再上传！");
				}
				// 设置文件缓存处理进度为0
				FileProcessProgressCache.putValue(ConvertUtil.convertToString(notification.get("identifier")), 0, 0,
						"");
				// 提交到线程。进行图片转换
				this.processList.add(notification);
				executorService.submit(new FileProcessTask(notification));
				return true;
			} else {
				return true;
			}
		} catch (Exception e) {
			log.error("文件" + notification.get("identifier") + "转换错误,异常信息：" + e.getMessage());
			throw new ServiceException(e.getMessage());
		}
	}

	/**
	 * 查询文件是否在处理中
	 * 
	 * @description:
	 * @return
	 * @author: donghang
	 * @date: 2021年5月13日上午11:22:22
	 * @modify:
	 */
	public boolean isExistHandleringFile(Map<String, Object> notification) {
		boolean isExistFile = false;
		for (Map<String, Object> existMap : this.processList) {
			if (ConvertUtil.convertToString(notification.get("identifier"))
					.equals(ConvertUtil.convertToString(existMap.get("identifier")))) {
				isExistFile = true;
				break;
			}
		}
		return isExistFile;
	}

	/**
	 * 删除处理的文件
	 * 
	 * @description:
	 * @author: donghang
	 * @date: 2021年5月13日上午11:28:01
	 * @modify:
	 */
	public void deleteFile(Map<String, Object> notification) {
		for (int i = this.processList.size() - 1; i >= 0; i--) {
			if (ConvertUtil.convertToString(notification.get("identifier"))
					.equals(ConvertUtil.convertToString(notification.get("identifier")))) {
				this.processList.remove(i);
			}
		}
	}
}
